<!DOCTYPE html>
<html>
<head>
<head />
<div th:include="base-libs :: base-libs" />
<title>字典表操作</title>
<style type="text/css">
</style>
</head>
<script th:inline="javascript">
	/**字典表名*/
	var ZDBM = [[${ZDBM}]];
	/**是否单级值域*/
	var SFDJZY = [[${SFDJZY}]];
</script>
<script type="text/javascript">
	$(function() {
		initView();
		//initData();
	});
	
	function initView() {
		if(SFDJZY == "0"){
			$("#dgForm").treegrid({
				idField : "XMDM",
				treeField: "XMMC",
				striped : true,
				border:false,
				clickToSelect: true,
				columns : [[
					{
						field : "XMDM",
						width: 200,
						align: "center",
						title : "项目代码"
					},
					{
						field : "XMMC",
						width: 300,
						title : "项目名称"
					},
					{
						field : "XMSM",
						width: 300,
						align: "center",
						title : "项目说明"
					},
					{
						field : "SJDM",
						width: 100,
						align: "center",
						title : "上级代码"
					},
					{
						field : "JLZT",
						width: 200,
						align: "center",
						title : "记录状态",
						formatter: function(value, row, index){
							if(value == "0"){
								value = "停用";
							}else if(value == "1"){
								value = "启用";
							}
							return value;
						}
					}
				]]
				/* ,pagination : true
				,pageNumber: 1
				,pageSize: 100 */
				,fit: true
				,fitColumns: true
				,singleSelect: true
				,rownumbers: true
				//,url: "queryTable"
				,onBeforeLoad: function(row, params)//加载之前回调 获取查询条件
				{
					params["TABLENAME"] = ZDBM;
					params["XMMC"] = $("#XMMC").val();
					return params;
				} ,
				onLoadSuccess: function(row, data){
					/* console.log(data);
					return ; */
				} 
			});
			queryTable();
		}
		else if(SFDJZY == "1"){
			/**加载easyui datagrid*/
			$("#dgForm").datagrid({
					idField : "XMDM",
					uniqueId: "XMDM",
					striped : true,
					border:false,
					clickToSelect: true,
					columns : [[
						{
							field : "XMDM",
							width: 200,
							align: "center",
							title : "项目代码"
						},
						{
							field : "XMMC",
							width: 300,
							align: "center",
							title : "项目名称"
						},
						{
							field : "XMSM",
							width: 300,
							align: "center",
							title : "项目说明"
						},
						{
							field : "JLZT",
							width: 200,
							align: "center",
							title : "记录状态",
							formatter: function(value, row, index){
								if(value == "0"){
									value = "停用";
								}else if(value == "1"){
									value = "启用";
								}
								return value;
							}
						}
					]]
					,pagination : true
					,pageNumber: 1
					,pageSize: 10
					,fit: true
					,fitColumns: true
					,singleSelect: true
					,rownumbers: true
					,url: "queryTable.service"
					,onBeforeLoad:function(params)//加载之前回调 获取查询条件
					{
						params["TABLENAME"] = ZDBM;
						params["XMMC"] = $("#XMMC").val();
						return params;
					}/* ,
					onLoadSuccess: function(data){
					} */
				});
		}
	
		
		/**查询按钮*/
		$("#btnQuery").on("click", function(){
			if(SFDJZY == "1"){
				$("#dgForm").datagrid("reload");
			}else{
				queryTable();//$("#dgForm").treegrid("reload");
			}
		});
		
		/**新增按钮*/
		$("#btnInsert").on("click", function(){
			parent.layer.open({
				  type: 1,
				  area: ["80%", "330px"], //宽高
				  scrollbar: false,
				  resize: false,
				  content: $("#managerDiv").html(),
				  btn: ["确定", "取消"],
				  success: function(layero, index){
					    if(SFDJZY == "1"){
					    	$(layero).find("#SJDM").remove();
					    }else{
					    	var row = $("#dgForm").treegrid("getSelected");
					    	if(row){
					    		$(layero).find(".SJDM").val(row["XMDM"]);
							  	$(layero).find(".SJDM").attr("readOnly", "readOnly");
					    	}
					    }
				  },
				  yes: function (index, layero) {
	           			if($(layero).find(".form-control").QuickValidate()){
	           				var params = {
	           						XMDM : $(layero).find(".XMDM").val(),
        							XMMC : $(layero).find(".XMMC").val(),
        							JLZT : $(layero).find(".JLZT").val()
        						}; 
	           				params["TABLENAME"] = ZDBM;
	           				params["SFDJZY"] = SFDJZY;
	           				if(SFDJZY == "1"){
	           					params["XMSM"] = $(layero).find("input[name='XMSM']:eq(0)").val();
	           				}else{
	           					params["SJDM"] = $(layero).find("input[name='SJDM']:eq(0)").val();
	           				}
	        				$.QuickRemote.AjaxFiledFormSubmit("insertForm", function(result) { 
	        					var info = result["info"];
	        					if (result["state"]) {
	        						$.QuickAlert.alertNormal("操作成功！",{callback: function(){
	        							parent.layer.close(index);//这块是点击确定关闭这个弹出层
	        							$("#btnQuery").trigger("click");
	        						}});
	        					} else {
	        						$.QuickAlert.alertFail({
	        							content : info["faultInfo"]
	        						});
	        					}
	        				}, params);
	        			} 
	           		}
				});
		});
		
		/**修改按钮*/
		$("#btnUpdate").on("click", function(){
			var row = $("#dgForm").datagrid("getSelected");
			if(row){
				parent.layer.open({
					  type: 1,
					  area: ["80%", "330px"], //宽高
					  scrollbar: false,
					  resize: false,
					  content: $("#managerDiv").html(),
					  btn: ["确定", "取消"],
					  success: function(layero, index){
						  if(SFDJZY == "1"){
						    	$(layero).find("#SJDM").remove();
						    }
						  	$(layero).find(".XMDM").val(row["XMDM"]);
						  	$(layero).find(".XMMC").val(row["XMMC"]);
						  	$(layero).find(".JLZT").val(row["JLZT"]);
						  	$(layero).find(".XMDM").attr("readOnly", "readOnly");
						  	if(SFDJZY == "1"){
	           					$(layero).find("input[name='XMSM']:eq(0)").val(row["XMSM"]);
	           				}else{
	           					$(layero).find("input[name='SJDM']:eq(0)").val(row["SJDM"]);
	           				}
					  },
					  yes: function (index, layero) {
		           			if($(layero).find(".form-control").QuickValidate()){
		           				var params = {
		           						XMDM : $(layero).find(".XMDM").val(),
		           						XMMC : $(layero).find(".XMMC").val(),
	        							JLZT : $(layero).find(".JLZT").val()
	        						}; 
		           				params["TABLENAME"] = ZDBM;
		           				params["SFDJZY"] = SFDJZY;
		           				if(SFDJZY == "1"){
		           					params["XMSM"] = $(layero).find("input[name='XMSM']:eq(0)").val();
		           				}else{
		           					params["SJDM"] = $(layero).find("input[name='SJDM']:eq(0)").val();
		           				}
		        				$.QuickRemote.AjaxFiledFormSubmit("updateForm", function(result) { 
		        					var info = result["info"];
		        					if (result["state"]) {
		        						$.QuickAlert.alertNormal("操作成功！",{callback: function(){
		        							parent.layer.close(index);//这块是点击确定关闭这个弹出层
		        							$("#btnQuery").trigger("click");
		        						}});
		        					} else {
		        						$.QuickAlert.alertFail({
		        							content : info["faultInfo"]
		        						});
		        					}
		        				}, params);
		        			} 
		           		}
					});
			}else{
				$.QuickAlert.alertFail({
					content : "请选中需要修改的记录！"
				});
			}
		});
		
		/**删除按钮*/
		$("#btnDelete").on("click", function(){
			var row = $("#dgForm").datagrid("getSelected");
			if(row){
				$.QuickAlert.alertConfirm({content: "确认是否删除记录？", callback: function(){
					var params = {
							TABLENAME : ZDBM,
							SFDJZY : SFDJZY,
							XMDM : row["XMDM"]
					};
					$.QuickRemote.AjaxFiledFormSubmit("deleteForm", function(result) { 
						var info = result["info"];
						if (result["state"]) {
								$.QuickAlert.alertNormal("删除成功！",{callback: function(){
								$("#btnQuery").trigger("click");
							}});
						} else {
							$.QuickAlert.alertFail({
								content : info["faultInfo"]
							});
						}
					}, params);
				}});
			}else{
				$.QuickAlert.alertFail({
					content : "请选中需要删除的记录！"
				});
			}
		});
		
		/**返回按钮*/
		$("#btnBack").on("click", function(){
			$.QuickUrlUtils.redirectUrl("index.html");
		});
	}
	
	/**封装树形结构*/
	function createTreeList(list, SJDM) {
		var childList = [];
		for (var i = 0; i < list.length; i++) {
			var item = list[i];
			if (item == null) {
				continue;
			}
			var SJDM1 = item["SJDM"];
			if (SJDM1 != SJDM ) {
				continue;
			} else {
				var resid = item["XMDM"];
				var lt = createTreeList(list, resid);
				if (lt != null && lt.length != 0) {
					item["children"] = lt;
				}
				childList.push(item);
				list[i] = null;
			}
		}
		if (childList.length == 0) {
			childList = null;
		}
		return childList;
	}
	
	function queryTable(){
		$.QuickRemote.AjaxFiledFormSubmit("queryForm", function(ary) { 
			if (ary.length > 0) {
				$("#dgForm").treegrid("loadData", createTreeList(ary, ""));
			} 
		}, {TABLENAME : ZDBM, XMMC : $("#XMMC").val()});
	}
</script>

<body>
<div  class="easyui-layout" fit="true">
	<div region="north" style="border: none; overflow: hidden;">
		<h4 class="h4-title" style="font-size: 28px;">
			字典项目信息
			<div style="float:right;margin-top:-7px;">
				<button class="btn" type="button" id="btnInsert" >新增</button>
				&nbsp;
				<button class="btn" type="button" id="btnUpdate" >修改</button>
				&nbsp;
				<button class="btn" type="button" id="btnDelete" >删除</button>
				&nbsp;
				<button class="btn" type="button" id="btnBack" >返回</button>
			</div>
		</h4>
		<div class="form-table-box" >
			<form id="queryForm" action="queryTable"></form>
			<form id="insertForm" action="insertOne"></form>
			<form id="updateForm" action="updateOne"></form>
			<form id="deleteForm" action="deleteOne"></form>
			<table class="form-table">
				<tr>
					<td class="td-text">项目名称：</td>
					<td class="td-content">
						<input id="XMMC" name="XMMC" class="form-control" type="text" placeholder="项目名称">
					</td>
					<td>
						<button class="btn" type="button" id="btnQuery">查询</button>
					</td>
				</tr>
			</table>
		</div>
	</div>
	<div region="center" style="border: none;">
		<table id="dgForm">
		</table>
</div>
</div>
<script type="text/html" id="ycBtn">
<a class="tableToolBarBtn" href="javascript:void(0)" title="移除" onclick="remove(#index#)">移除</a>
</script>
<script type="text/html" id="managerDiv">
<div  class="easyui-layout">
	<div region="north" style="border: none; overflow: hidden;">
	<h4 class="h4-title" style="font-size: 28px;text-align:center;">
		字典表信息
	</h4>
	</div>
	<div region="center" style="border: none">
	<div class="form-table-box" >
		<form id="managerForm" action="" class="form-horizontal" >
			<table class="form-table">
					<tr>
						<td class="td-text">项目代码：</td>
						<td class="td-content">
							<input name="XMDM" class="form-control XMDM" type="text"
								quick-config="label:'项目代码', validateProxy:'notNull length', length: '20'"
								placeholder="项目代码">
						</td>
						<td class="td-text">项目名称：</td>
						<td class="td-content">
							<input name="XMMC" class="form-control XMMC" type="text"
								quick-config="label:'项目名称', validateProxy:'notNull length', length: '25'"
								placeholder="项目名称">
						</td>
					</tr>
					
					<tr>
						<td class="td-text">项目说明：</td>
						<td class="td-content">
							<input name="XMSM" class="form-control XMSM" type="text"
								quick-config="label:'项目说明', validateProxy:'length', length: '50'"
								placeholder="项目说明">
						</td>
						<td class="td-text">记录状态：</td>
						<td class="td-content">
							<select class="form-control JLZT" name="JLZT"
								quick-config="label:'记录状态', validateProxy:'notNull'">
								<option value="1">启用</option>
								<option value="0">停用</option>
							</select>
						</td>
					</tr>
					<tr id="SJDM">
						<td class="td-text">上级代码：</td>
						<td class="td-content">
							<input name="SJDM" class="form-control SJDM" type="text"
								quick-config="label:'上级代码', validateProxy:'length', length: '50'"
								placeholder="上级代码">
						</td>
					</tr>
				</table>
		</form>
	</div>
</div>
</script>
</body>
</html>


